package com.xd.springaiagent.config;

import com.xd.springaiagent.tools.FileTool;
import com.xd.springaiagent.tools.Terminate;
import com.xd.springaiagent.tools.WeatherTool;
import com.xd.springaiagent.tools.WebSearchTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;

import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class aiConfig {

    @Bean
    public ToolCallbackProvider toolCallbackProvider(
            WebSearchTool webSearchTool, WeatherTool weatherTool, FileTool fileTool,
            Terminate terminate) {
        return MethodToolCallbackProvider.builder().toolObjects(webSearchTool, weatherTool, fileTool, terminate).build();
    }

//    @Bean
//    public ChatMemory chatMemory() {
//        return new InMemoryChatMemory();
//    }

    @Bean
    public ChatClient chatClient(OllamaChatModel openAiChatModel, ChatMemory chatMemory) {;
        ChatOptions options = ToolCallingChatOptions.builder()
                .internalToolExecutionEnabled(false)
                .build();
        return ChatClient.builder(openAiChatModel)
                .defaultAdvisors(
//                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultOptions(options)
                .build();
    }
}
